package pro.model.tests;

import java.util.*;
import junit.framework.TestCase;
import pro.model.core.*;


/**
 * @author  sangwo.y
 */
public class GameTest extends TestCase  {

	private Card AS;
	private Card AD;
	private Card AH;
	private Card AC;
	private Card KS;
	private Card KD;
	private Card KH;
	private Card KC;
	private Card QS;
	private Card QD;
	private Card QH;
	private Card QC;
	private Card JS;
	private Card JD;
	private Card JH;
	private Card JC;
	private Card TenS;
	private Card TenD;
	private Card TenH;
	private Card TenC;
	private Card NineS;
	private Card NineD;
	private Card NineH;
	private Card NineC;
	private Card EightS;
	private Card EightD;
	private Card EightH;
	private Card EightC;
	private Card SevenS;
	private Card SevenD;
	private Card SevenH;
	private Card SevenC;
	private Card SixS;
	private Card SixD;
	private Card SixH;
	private Card SixC;
	private Card FiveS;
	private Card FiveD;
	private Card FiveH;
	private Card FiveC;
	private Card FourS;
	private Card FourD;
	private Card FourH;
	private Card FourC;
	private Card ThreeS;
	private Card ThreeD;
	private Card ThreeH;
	private Card ThreeC;
	private Card TwoS;
	private Card TwoD;
	private Card TwoH;
	private Card TwoC;
		
	private List<Card> theDeck;
	private Game game;
	
	
	protected void setUp() throws Exception {
		super.setUp();
		initCards();
		fillTheDeck();
		game = new Game();
		game.dealHands(theDeck);
	}
	
	public GameTest(String name) {
		super(name);
	}
	
	public void testGame() {
		/*System.out.println(game.toString());
		game.moveFromPile(8, 2);
		System.out.println(game.toString());
		game.moveToPile(5);
		System.out.println(game.toString());
		game.moveFromPile(8, 1);
		System.out.println(game.toString());
		game.moveToFreeCell(Game.Cell.FREE_CELL_ONE);
		System.out.println(game.toString());
		game.moveFromPile(8, 1);
		System.out.println(game.toString());
		game.moveToFreeCell(Game.Cell.FREE_CELL_TWO);
		System.out.println(game.toString());
		game.moveFromPile(8, 1);
		System.out.println(game.toString());
		game.moveToFreeCell(Game.Cell.FREE_CELL_THREE);
		System.out.println(game.toString());		
		game.moveFromPile(8, 1);
		System.out.println(game.toString());
		game.moveToFreeCell(Game.Cell.FREE_CELL_FOUR);
		System.out.println(game.toString());
		game.moveFromPile(7, 2);
		System.out.println(game.toString());
		game.moveToPile(8);
		System.out.println(game.toString());
		
		System.out.println(game.toString());
		game.moveFromPile(3, 1);
		System.out.println(game.toString());
		game.moveToFreeCell(Game.Cell.FREE_CELL_TWO);
		System.out.println(game.toString());
		game.moveFromFreeCell(Game.Cell.FREE_CELL_TWO);
		System.out.println(game.toString());
		game.moveToPlayedCards();
		System.out.println(game.toString());
		game.moveFromPile(1, 1);
		System.out.println(game.toString());
		game.moveToPlayedCards();
		System.out.println(game.toString());*/
		
	}
	
	public void testMoveToPlayedCards() {
		System.out.println("--------------------- testMoveToPlayedCards---------------------------");
		System.out.println(game);
		//nothing to move
		try {
			game.moveToPlayedCards();
		} catch (Exception e) {System.out.println(e);}
		//too much to move
		try {
			game.moveFromPile(8, 2);
			System.out.println(game);
			game.moveToPlayedCards();
		} catch (Exception e) {System.out.println(e);}
		//ok
		try {
			game.moveToPile(5);
			System.out.println(game);
			game.moveFromPile(3, 1);
			assertTrue(game.moveToPlayedCards());
			System.out.println(game);
			game.moveFromPile(2, 1);
			System.out.println(game);
			assertTrue(game.moveToPlayedCards());
			System.out.println(game);
		} catch (Exception e) {fail();}
		
		//place first - no ace, nok
		try {
			game.moveFromPile(7, 1);
			assertFalse(game.moveToPlayedCards());			
		} catch (Exception e) {fail();}
		//place card - not sameNext, nok
		try {
			System.out.println(game);
			game.moveFromPile(8, 1);
			System.out.println(game);
			assertFalse(game.moveToPlayedCards());			
		} catch (Exception e) {fail();}		

		System.out.println("--------------------- testMoveToPlayedCards---------------------------");
	}
	
	
	public void testMoveToFreeCell() {
		System.out.println("--------------------- testMoveToFreeCell---------------------------");
		System.out.println(game);
		//nothing or too much to move
		try {
			game.moveToFreeCell(Game.Cell.FREE_CELL_ONE);
			fail();
		} catch (Exception e) {System.out.println(e);}
		try {
			game.moveFromPile(8,2);
			System.out.println(game);
			game.moveToFreeCell(Game.Cell.FREE_CELL_ONE);
			fail();
		} catch (Exception e) {System.out.println(e);}
		//cell occupied
		try {
			game.moveToPile(5);
			game.moveFromPile(8, 1);
			game.moveToFreeCell(Game.Cell.FREE_CELL_ONE);
			game.moveFromPile(8, 1);
			System.out.println(game);
			assertFalse(game.moveToFreeCell(Game.Cell.FREE_CELL_ONE));
			//assertFalse(game.moveToFreeCell(Game.Cell.FREE_CELL_ONE));
		} catch(Exception e) {fail();}
		System.out.println("--------------------- testMoveToFreeCell---------------------------");
	}
	
	public void testMoveFromFreeCell() {
		System.out.println("--------------------- testMoveFromFreeCell---------------------------");
		System.out.println(game);
		//empty cell err
		try {
			game.moveFromFreeCell(Game.Cell.FREE_CELL_ONE);
			fail();
		} catch (Exception e) {System.out.println(e);}
		//other card(s) already in play
		try {
			game.moveFromPile(8, 1);
			game.moveToFreeCell(Game.Cell.FREE_CELL_ONE);
			game.moveFromPile(8,1);
			System.out.println(game);
			game.moveFromFreeCell(Game.Cell.FREE_CELL_ONE);
			fail();
		} catch (Exception e) {System.out.println(e);}
		System.out.println("--------------------- testMoveFromFreeCell---------------------------");
	}
	
	public void testMoveToPile() {
		System.out.println("--------------------- testMoveToPile---------------------------");
		System.out.println(game);
		//wrong parameters
		try {
			game.moveFromPile(4, 1);
			System.out.println(game);
			game.moveToPile(0);
		} catch (Exception e) {System.out.println(e);}
		try {
			game.moveToPile(10);
		} catch (Exception e) {System.out.println(e);}
		
		//ok moving 1 card & nok nothing to move
		try {
			game.moveToPile(1);
			System.out.println(game);
			game.moveToPile(2);
		} catch (Exception e) {System.out.println(e);}
		
		//seq not accepted by column
		
		try {
			game.moveFromPile(1, 2);
			System.out.println(game);
			assertFalse(game.moveToPile(3));
			System.out.println(game);
		} catch (Exception e) {fail();}
		
		System.out.println("--------------------- testMoveToPile---------------------------");		
	}
	
	public void testMoveFromPile() {
		System.out.println("--------------------- testMoveFromPile---------------------------");
		System.out.println(game.toString());		
		//wrong parameters (out of scope)
		try {game.moveFromPile(1,0); fail();} 
			catch (Exception e) {System.out.println(e);}
		try {game.moveFromPile(1,20); fail();} 
			catch (Exception e) {System.out.println(e);}
		try {game.moveFromPile(0,1); fail();} 
			catch (Exception e) {System.out.println(e);}
		try {game.moveFromPile(0,20); fail();} 
			catch (Exception e) {System.out.println(e);}
			
		//empty stack
		try {
			game.moveFromPile(8,1);
			game.moveToFreeCell(Game.Cell.FREE_CELL_ONE);
			game.moveFromPile(8,1);
			game.moveToFreeCell(Game.Cell.FREE_CELL_TWO);
			game.moveFromPile(8,1);
			game.moveToFreeCell(Game.Cell.FREE_CELL_THREE);
			game.moveFromPile(8,1);
			game.moveToPlayedCards();
			game.moveFromPile(8,1);
			game.moveToFreeCell(Game.Cell.FREE_CELL_FOUR);
			game.moveFromPile(8,1);
			game.moveToPlayedCards();				
			System.out.println(game); 
			game.moveFromPile(8, 1);
			fail();
		} catch (Exception e) {System.out.println(e);}	
		
		//enough / not enough freecells 
		try {
			assertNotNull(game.moveFromPile(4, 1));
			game.moveToPile(1);
			System.out.println(game);
			game.moveFromPile(1, 2);
			fail();
		}	catch (Exception e) {System.out.println(e);}
		
		try {
			game.moveFromFreeCell(Game.Cell.FREE_CELL_FOUR);
			game.moveToPile(8);
			//list cards - ok
			assertNotNull(game.moveFromPile(1, 2));
			System.out.println(game);
			game.moveToPile(4);
			System.out.println(game);
		} catch (Exception e) {fail();}
			
		//invalid sequence
		
		try {
			game.moveFromPile(3, 2);
			fail();
		} catch(Exception e) {System.out.println(e);}
		
		//current list busy
		try {
			game.moveFromPile(1, 1);
			game.moveFromPile(1, 1);
		} catch(Exception e) {System.out.println(e);}
		
		System.out.println("--------------------- testMoveFromPile---------------------------");
	}
	
	private void fillTheDeck() {
		theDeck = new ArrayList<Card>();
		theDeck.add(KD); theDeck.add(SixS); theDeck.add(NineH); theDeck.add(EightC); 
		theDeck.add(NineS); theDeck.add(SevenS); theDeck.add(QH);
		
		theDeck.add(TwoS); theDeck.add(TwoC); theDeck.add(FourD); theDeck.add(FourC); 
		theDeck.add(NineC); theDeck.add(JH); theDeck.add(KH);
		
		theDeck.add(JD); theDeck.add(QS); theDeck.add(TenH); theDeck.add(ThreeS); 
		theDeck.add(ThreeD); theDeck.add(FiveD); theDeck.add(AH);
		
		theDeck.add(KC); theDeck.add(AS); theDeck.add(ThreeH); theDeck.add(EightS); 
		theDeck.add(EightD); theDeck.add(KS); theDeck.add(JS);
		
		
		theDeck.add(SevenD); theDeck.add(ThreeC); theDeck.add(QD); theDeck.add(TwoD); 
		theDeck.add(TenS); theDeck.add(SevenC);
		
		theDeck.add(SixD); theDeck.add(FourS); theDeck.add(JC); theDeck.add(FiveH); 
		theDeck.add(FourH); theDeck.add(SixC); 
		
		theDeck.add(EightH); theDeck.add(NineD); theDeck.add(QC); theDeck.add(SevenH); 
		theDeck.add(TwoH); theDeck.add(TenC); 
		
		theDeck.add(AC); theDeck.add(FiveC); theDeck.add(AD); theDeck.add(TenD); 
		theDeck.add(SixH); theDeck.add(FiveS); 

	}
	
	
	private void  initCards() {
		AS = new Card(Card.Suit.Spade, Card.Value.Ace);
		AD = new Card(Card.Suit.Diamond, Card.Value.Ace);
		AH = new Card(Card.Suit.Heart, Card.Value.Ace);
		AC = new Card(Card.Suit.Club, Card.Value.Ace);
		KS = new Card(Card.Suit.Spade, Card.Value.King);
		KD = new Card(Card.Suit.Diamond, Card.Value.King);
		KH = new Card(Card.Suit.Heart, Card.Value.King);
		KC = new Card(Card.Suit.Club, Card.Value.King);
		QS = new Card(Card.Suit.Spade, Card.Value.Queen);
		QD = new Card(Card.Suit.Diamond, Card.Value.Queen);
		QH = new Card(Card.Suit.Heart, Card.Value.Queen);
		QC = new Card(Card.Suit.Club, Card.Value.Queen);
		JS = new Card(Card.Suit.Spade, Card.Value.Jack);
		JD = new Card(Card.Suit.Diamond, Card.Value.Jack);
		JH = new Card(Card.Suit.Heart, Card.Value.Jack);
		JC = new Card(Card.Suit.Club, Card.Value.Jack);
		
		TenS = new Card(Card.Suit.Spade, Card.Value.Ten);
		TenD = new Card(Card.Suit.Diamond, Card.Value.Ten);
		TenH = new Card(Card.Suit.Heart, Card.Value.Ten);
		TenC = new Card(Card.Suit.Club, Card.Value.Ten);
		
		NineS = new Card(Card.Suit.Spade, Card.Value.Nine);
		NineD = new Card(Card.Suit.Diamond, Card.Value.Nine);
		NineH = new Card(Card.Suit.Heart, Card.Value.Nine);
		NineC = new Card(Card.Suit.Club, Card.Value.Nine);
		
		EightS = new Card(Card.Suit.Spade, Card.Value.Eigth);
		EightD = new Card(Card.Suit.Diamond, Card.Value.Eigth);
		EightH = new Card(Card.Suit.Heart, Card.Value.Eigth);
		EightC = new Card(Card.Suit.Club, Card.Value.Eigth);
		
		SevenS = new Card(Card.Suit.Spade, Card.Value.Seven);
		SevenD = new Card(Card.Suit.Diamond, Card.Value.Seven);
		SevenH = new Card(Card.Suit.Heart, Card.Value.Seven);
		SevenC = new Card(Card.Suit.Club, Card.Value.Seven);
		
		SixS = new Card(Card.Suit.Spade, Card.Value.Six);
		SixD = new Card(Card.Suit.Diamond, Card.Value.Six);
		SixH = new Card(Card.Suit.Heart, Card.Value.Six);
		SixC = new Card(Card.Suit.Club, Card.Value.Six);
		
		FiveS = new Card(Card.Suit.Spade, Card.Value.Five);
		FiveD = new Card(Card.Suit.Diamond, Card.Value.Five);
		FiveH = new Card(Card.Suit.Heart, Card.Value.Five);
		FiveC = new Card(Card.Suit.Club, Card.Value.Five);	
		
		FourS = new Card(Card.Suit.Spade, Card.Value.Four);
		FourD = new Card(Card.Suit.Diamond, Card.Value.Four);
		FourH = new Card(Card.Suit.Heart, Card.Value.Four);
		FourC = new Card(Card.Suit.Club, Card.Value.Four);	
		
		ThreeS = new Card(Card.Suit.Spade, Card.Value.Three);
		ThreeD = new Card(Card.Suit.Diamond, Card.Value.Three);
		ThreeH = new Card(Card.Suit.Heart, Card.Value.Three);
		ThreeC = new Card(Card.Suit.Club, Card.Value.Three);		
		
		TwoS = new Card(Card.Suit.Spade, Card.Value.Two);
		TwoD = new Card(Card.Suit.Diamond, Card.Value.Two);
		TwoH = new Card(Card.Suit.Heart, Card.Value.Two);
		TwoC = new Card(Card.Suit.Club, Card.Value.Two);	
		
	}
}
